﻿using Microsoft.Owin.Security;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Sustainalytics.LongRunningTaskManager.Interface
{
    public interface ILongRunningTaskManager
    {
        /// <summary>
        /// Task manager settings.
        /// </summary>
        ILongRunningTaskManagerSettings Settings { get; }

        /// <summary>
        /// Main cancellation token.
        /// </summary>
        CancellationToken MainCancellationToken { get; }

        /// <summary>
        /// Current task factory.
        /// </summary>
        ILongRunningTaskFactory TaskFactory { get; }


        /// <summary>
        /// Checks whether max number of users has exceeded.
        /// </summary>
        void CheckMaxTasksPerUserReachedException(string username);

        /// <summary>
        ///  Start task instance.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <param name="title"> Task title. </param>
        /// <param name="paramJson"> Parameter (JSON) that will be passed to task creation. </param>
        /// <param name="taskType"> Task type to be instantiated. </param>
        /// <returns></returns>
        string Start(string id, string title, AuthenticationTicket ticket, string paramJson, LongRunningTaskTypeEnum taskType, bool isUpload, bool isDownload, long? order);

        /// <summary>
        /// Get overall status of current task instances running for a specific username.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <returns>  </returns>
        List<LongRunningTaskInstanceStatus> GetOverallStatus(AuthenticationTicket ticket);

        /// <summary>
        /// Get long running task status.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <param name="id"> Task instance ID. </param>
        /// <returns> Task instance status. </returns>
        LongRunningTaskInstanceStatus GetStatus(AuthenticationTicket ticket, string id);

        /// <summary>
        /// Get task instance result.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <param name="id"> Task instance ID. </param>
        /// <returns> Task instance result. </returns>
        ILongRunningTaskResult GetResult(AuthenticationTicket ticket, string id);

        /// <summary>
        /// Cancel a specific task instance.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <param name="id"> Task instance ID. </param>
        /// <returns> Managed Task which cancells the task instance. </returns>
        Task Cancel(AuthenticationTicket ticket, string id);

        void CancelAndWait(AuthenticationTicket ticket, string taskInstanceId);

        /// <summary>
        /// Cancel and remove requested task instance.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <param name="taskInstanceId"> Task instance ID. </param>
        /// <returns> Managed task which cancels and then removes the task instance. </returns>
        Task Remove(AuthenticationTicket ticket, string taskInstanceId);

        /// <summary>
        /// Get task async info.
        /// </summary>
        /// <param name="ticket"> Authentication ticket. </param>
        /// <param name="taskInstanceId"> Task instance ID. </param>
        /// <returns> Managed task. </returns>
        ILongRunningTaskInstanceAsyncInfo GetTaskAsyncInfo(AuthenticationTicket ticket, string taskInstanceId);

        /// <summary>
        /// Stops current manager.
        /// </summary>
        /// <returns> Task that stops the current manager. </returns>
        Task StopManager();

        /// <summary>
        /// Stop manager and wait.
        /// </summary>
        void StopManagerAndWait();
    }
}
